
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using AnovoAndes.Framework.Utilidades;
using NovaSystem.EntidadesNegocio;

namespace NovaSystem.AccesoDatos
{
	public class comprobanteDAO 
	{
			
		#region [Variables Publicas]
        List<string> listaEsquema = new List<string>();
		System.Configuration.AppSettingsReader read = new System.Configuration.AppSettingsReader();
        #endregion
		
		#region Constructores
		public comprobanteDAO()
		{
		}
		#endregion
		
		#region Metodos Privados
		
		 private List<string> LlenarEsquema(IDataReader dr)
        {
            DataTable schemaTable;
            schemaTable = dr.GetSchemaTable();
            listaEsquema = new List<string>();
            const int columnaNombre = 0;
            //Para cada campo de la tabla...
            foreach (DataRow myField in schemaTable.Rows)
            {
                //Agrega nombre del campo a la lista generica.
                listaEsquema.Add(myField[columnaNombre].ToString());
            }
            return listaEsquema;

        }
		
		private comprobante Crear(IDataReader dr)
		{
			comprobante vObj = new comprobante();
			try
			{
				listaEsquema.Sort();
							if (listaEsquema.BinarySearch("idComprobante",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["idComprobante"] is System.DBNull))
					{
						vObj.idComprobante= (int)dr["idComprobante"];
					}
				}
				if (listaEsquema.BinarySearch("numeroComprobante",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["numeroComprobante"] is System.DBNull))
					{
						vObj.numeroComprobante= (int)dr["numeroComprobante"];
					}
				}
				if (listaEsquema.BinarySearch("concepto",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["concepto"] is System.DBNull))
					{
						vObj.concepto= (string)dr["concepto"];
					}
				}
				if (listaEsquema.BinarySearch("fechaComprobante",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["fechaComprobante"] is System.DBNull))
					{
						vObj.fechaComprobante= (DateTime)dr["fechaComprobante"];
					}
				}
				if (listaEsquema.BinarySearch("fechaRegistro",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["fechaRegistro"] is System.DBNull))
					{
						vObj.fechaRegistro= (DateTime)dr["fechaRegistro"];
					}
				}
				if (listaEsquema.BinarySearch("usuario",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["usuario"] is System.DBNull))
					{
						vObj.usuario= (string)dr["usuario"];
					}
				}

				return vObj;
			}
			catch(Exception ex)
			{
				throw new Exception(ex.Message);
			}
		}
		
		private List<comprobante> CrearDTOs(IDataReader dr) 
		{
			List<comprobante> listaDTO = new List<comprobante>();
			if 	(dr != null)
			{
				listaEsquema = LlenarEsquema(dr);// Validadores.LlenarEsquema(dr);
				
				while (dr.Read())
				{
					listaDTO.Add(Crear(dr));
				}
				dr.Close();
			}
			return listaDTO;
		}
		
		#endregion
		
		#region Metodos Publicos
		public List<comprobante> Listar()
		{
			MySqlConnection conn = new MySqlConnection();
			List<comprobante> lista = new List<comprobante>();
			try
			{
				string connString = read.GetValue("Database",typeof(string)).ToString();
				conn.ConnectionString = connString;
				conn.Open();
				MySqlCommand objDbCommand = new MySqlCommand();
				objDbCommand.Connection = conn;
				objDbCommand.CommandType = CommandType.StoredProcedure;
				objDbCommand.CommandText = "usp_s_comprobante";
				
				using (IDataReader objDataReader = objDbCommand.ExecuteReader())
				{
					lista = CrearDTOs(objDataReader);
				}
				CerrarConn(conn);
				return lista;
			}
			catch(Exception ex)
			{
				CerrarConn(conn);
				throw new Exception(ex.Message);
			}
		}
		
		
		private void CerrarConn(MySqlConnection conn)
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Dispose();
            }
        }
		
		public int Insertar( comprobante dto)
		{
			MySqlConnection conn = new MySqlConnection();
			try
			{
				string connString = read.GetValue("Database",typeof(string)).ToString();
				conn.ConnectionString = connString;
				conn.Open();
				MySqlCommand objDbCommand = new MySqlCommand();
				objDbCommand.Connection = conn;
				objDbCommand.CommandType = CommandType.StoredProcedure;
				objDbCommand.CommandText = "usp_I_comprobante";
				objDbCommand.Parameters.AddWithValue("_idComprobante",AssertNull.getValue(dto.idComprobante));
				objDbCommand.Parameters.AddWithValue("_numeroComprobante",AssertNull.getValue(dto.numeroComprobante));
				objDbCommand.Parameters.AddWithValue("_concepto",AssertNull.getValue(dto.concepto));
				objDbCommand.Parameters.AddWithValue("_fechaComprobante",AssertNull.getValue(dto.fechaComprobante));
				objDbCommand.Parameters.AddWithValue("_usuario",AssertNull.getValue(dto.usuario));
                objDbCommand.Parameters["_idComprobante"].Direction = ParameterDirection.Output;
                objDbCommand.ExecuteNonQuery();
                int retorno = Convert.ToInt32(objDbCommand.Parameters["_idComprobante"].Value);
				CerrarConn(conn);
				return retorno;
			}
			catch(Exception ex)
			{
				CerrarConn(conn);
				throw new Exception(ex.Message);
			}
		}
		
		public void Eliminar(comprobante dto)
		{
			MySqlConnection conn = new MySqlConnection();
			try
			{
				string connString = read.GetValue("Database",typeof(string)).ToString();
				conn.ConnectionString = connString;
				conn.Open();
				MySqlCommand objDbCommand = new MySqlCommand();
				objDbCommand.Connection = conn;
				objDbCommand.CommandType = CommandType.StoredProcedure;
				objDbCommand.CommandText = "usp_D_comprobante";
								objDbCommand.Parameters.AddWithValue("_idComprobante",AssertNull.getValue(dto.idComprobante));

				objDbCommand.ExecuteNonQuery();
				CerrarConn(conn);
			}
			catch(Exception ex)
			{
				CerrarConn(conn);
				throw new Exception(ex.Message);
			}
		}
		
		#endregion
		
	}
}
